package com.baizhi.springboot.controller;

import com.baizhi.springboot.entity.Label;
import com.baizhi.springboot.service.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
@RestController
public class LabelController {
    @Autowired
    private LabelService labelService;

    @RequestMapping("/selectLabelByPage")
    public Map selectLabelByPage(Integer page,Integer limit) {
        return labelService.selectLabelByPage(page,limit);
    }
    @RequestMapping("/insertLabel")
    public Map insertLabel(Label label) {

        HashMap<Object, Object> map = new HashMap<>();

        try {
            labelService.insertLabel(label);
            map.put("isAdd", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isAdd", false);
        }
        return map;
    }
    @RequestMapping("/deleteLabel")
    public Map  deleteLabel(Integer id){
        HashMap<Object, Object> map = new HashMap<>();

        try {
            labelService.deleteLabel(id);
            map.put("isDelete", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isDelete", false);
        }
        return map;
    }

    @RequestMapping("/selectOneLabel")
    public Map selectOneLabel(Integer id, HttpSession session) {
        Label label= labelService.selectOneLabel(id);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("label", label);
        return map;
    }

    @RequestMapping("/updateLabel")
    public Map updateLabel(Label label) {
        HashMap<Object, Object> map = new HashMap<>();
        try {
            System.out.println(label);
            labelService.updateLabel(label);
            map.put("isUpdate", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isUpdate", false);
        }
        return map;
    }
}
